﻿using justice.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace justice.Controllers
{
    [Authorize]
    public class AsController : Controller
    {
        justiceEntities dal = new justiceEntities();
        //西科大鉴定材料流转单
        public ActionResult MakingsCirculation(int? shroffId)
        {
            if (shroffId != null)
            {
                Shroff_book shroff_book = dal.Shroff_book.ToList().Find(l => l.shroff_id == shroffId);//查委托书
                be_appraiser be_appraiser = dal.be_appraiser.ToList().Find(l => l.user_id == shroff_book.main_case);//根据委托书查被鉴定人
                MakingsCirculation mak = dal.MakingsCirculation.ToList().Find(l => l.ShroffId == shroffId);//查鉴定材料
                ViewBag.TableNumber = shroff_book.shroff_number;
                ViewBag.Client = shroff_book.client;
                ViewBag.be_appraiser_name = be_appraiser.username;
                //鉴定材料
                string[] makingsReceptionPeoples = new string[] { };
                Dictionary<string, string> m = new Dictionary<string, string>();
                if (mak.MakingsReceptionPeoples != null)
                {
                    makingsReceptionPeoples = mak.MakingsReceptionPeoples.Split('*'); foreach (var item in makingsReceptionPeoples)
                    {
                        if (item != "")
                        {
                            string[] s = item.Split(',');
                            m.Add(s[0], s[1]);
                        }
                    }
                }
                
                ViewBag.mak_ShroffMakings = mak.ShroffMakings;
                ViewBag.mak_Remarks = mak.Remarks;
                ViewBag.mak_MakingsReceptionPeoples = m;

                ViewBag.ShroffId = shroff_book.shroff_id;
                ViewBag.TableNumber = shroff_book.shroff_number;
                ViewBag.Client = shroff_book.client;
                ViewBag.be_appraiser_name = be_appraiser.username;
            }
            return View();
        }

        //提交西科大鉴定材料流转单
        public ActionResult SubmitMakingsCirculation(MakingsCirculation mak, List<string> MakingsReceptionPeoples)
        {
            StringBuilder m = new StringBuilder();
            MakingsCirculation _mak = dal.MakingsCirculation.ToList().Find(l => l.ShroffId == mak.ShroffId);
            for (int i = 0; i < MakingsReceptionPeoples.Count; i++)
            {
                if (MakingsReceptionPeoples[i] != "")
                {
                    if (i % 2 == 0)
                    {
                        m.Append(MakingsReceptionPeoples[i] + "," + MakingsReceptionPeoples[i + 1] + "*");
                    }
                }
            }
            mak.MakingsReceptionPeoples = m.ToString();

            _mak.MakingsReceptionPeoples = mak.MakingsReceptionPeoples;
            _mak.Remarks = mak.Remarks;
            _mak.ShroffMakings = mak.ShroffMakings;
            dal.SaveChanges();
            Response.Redirect("/As/ForensicExaminationRecord?shroffId=" + mak.ShroffId);
            return View();
        }

        //以数据展示西科大鉴定材料流转单
        public ActionResult MakingsCirculationDataView(int? id)
        {
            if (id != null)
            {
                Shroff_book shroff_book = dal.Shroff_book.ToList().Find(l => l.shroff_id == id);//查委托书
                be_appraiser be_appraiser = dal.be_appraiser.ToList().Find(l => l.user_id == shroff_book.main_case);//根据委托书查被鉴定人
                MakingsCirculation mak = dal.MakingsCirculation.ToList().Find(l => l.ShroffId == id);//查鉴定材料
                ViewBag.TableNumber = shroff_book.shroff_number;
                ViewBag.Client = shroff_book.client;
                ViewBag.be_appraiser_name = be_appraiser.username;
                //鉴定材料
                Dictionary<string, string> m = new Dictionary<string, string>();
                if (mak.MakingsReceptionPeoples != null)
                {
                    string[] makingsReceptionPeoples = mak.MakingsReceptionPeoples.Split('*');
                    foreach (var item in makingsReceptionPeoples)
                    {
                        if (item != "")
                        {
                            string[] s = item.Split(',');
                            m.Add(s[0], s[1]);
                        }
                    }
                }
                ViewBag.mak_ShroffMakings = mak.ShroffMakings;
                ViewBag.mak_Remarks = mak.Remarks;
                ViewBag.mak_MakingsReceptionPeoples = m;
                ViewBag.ShroffId = id;
            }
            return View();
        }

        //法医临床学检查笔录
        public ActionResult ForensicExaminationRecord(int? shroffId)
        {
            if (shroffId != null)
            {
                Shroff_book shroff_book = dal.Shroff_book.ToList().Find(l => l.shroff_id == shroffId);//查委托书
                be_appraiser be_appraiser = dal.be_appraiser.ToList().Find(l => l.user_id == shroff_book.main_case);//根据委托书查被鉴定人
                nationality nation = dal.nationality.ToList().Find(l => l.id == be_appraiser.nationality);//被鉴定人族别
                ForensicExaminationRecord foren = dal.ForensicExaminationRecord.ToList().Find(l => l.TableNumber == shroffId);//根据委托书查 法医临床学检查笔录
                ViewBag.ShroffId = shroff_book.shroff_id;//委托书id
                ViewBag.TableNumber = shroff_book.shroff_number;//委托书编号
                ViewBag.Client = shroff_book.client;//委托人
                ViewBag.be_appraiser = be_appraiser;//被鉴定人
                ViewBag.be_appraiser_nationality = nation.nationalityName;//族别
                ViewBag.Foren = foren;//法医临床学检查笔录信息
                //计算被鉴定人年龄
                string idcard = be_appraiser.ID_card.Substring(6, 8);
                string dt = DateTime.Now.ToString("yyyyMMdd");
                ViewBag.be_appraiser_age = ((Convert.ToInt32(dt) - Convert.ToInt32(idcard)).ToString()).Substring(0, 2);
                //法医临床学检查笔录信息 使用仪器
                string[] str = new string[] { };
                List<string> useTheInstrument = new List<string>();
                if (foren.UseTheInstrument != null)
                {
                    str = foren.UseTheInstrument.Split('*');
                    foreach (var item in str)
                    {
                        if (item != "")
                            useTheInstrument.Add(item);
                    }
                }
                ViewBag.UseTheInstrument = useTheInstrument;
            }
            return View();
        }

        //提交法医临床学检查笔录
        public ActionResult SubmitForensicExaminationRecord(ForensicExaminationRecord foren, string MethodOfUse2, List<string> UseTheInstrument)
        {
            StringBuilder UseTheInstruments = new StringBuilder();
            if (UseTheInstrument.Count > 0)
            {
                foreach (var item in UseTheInstrument)
                {
                    if (item != "")
                        UseTheInstruments.Append(item + "*");
                }
            }
            foren.MethodOfUse = foren.MethodOfUse == "on" ? MethodOfUse2 : foren.MethodOfUse;
            foren.UseTheInstrument = UseTheInstruments.ToString();
            ForensicExaminationRecord _foren = dal.ForensicExaminationRecord.ToList().Find(l => l.TableNumber == foren.TableNumber);
            _foren.Time = foren.Time;
            _foren.InjuryHistoryUser = foren.InjuryHistoryUser;
            _foren.Reliability = foren.Reliability;
            _foren.MethodOfUse = foren.MethodOfUse;
            _foren.UseTheInstrument = foren.UseTheInstrument;
            _foren.CheckTheSituation = foren.CheckTheSituation;
            _foren.InjuryHistory = foren.InjuryHistory;
            _foren.CheckUser = foren.CheckUser;
            _foren.Photographer = foren.Photographer;
            _foren.InspectedOrAttendees = foren.InspectedOrAttendees;
            _foren.remarks = foren.remarks;

            dal.SaveChanges();
            Response.Redirect("/As/ReceiptServiceEvaluations?shroffId=" + foren.TableNumber);
            return View();
        }

        //以数据展示法医临床学检查笔录
        public ActionResult ForensicExaminationRecordDataView(int? id)
        {
            if (id != null)
            {
                Shroff_book shroff_book = dal.Shroff_book.ToList().Find(l => l.shroff_id == id);//查委托书
                be_appraiser be_appraiser = dal.be_appraiser.ToList().Find(l => l.user_id == shroff_book.main_case);//根据委托书查被鉴定人
                nationality nation = dal.nationality.ToList().Find(l => l.id == be_appraiser.nationality);//被鉴定人族别
                ForensicExaminationRecord foren = dal.ForensicExaminationRecord.ToList().Find(l => l.TableNumber == id);//根据委托书查 法医临床学检查笔录
                ViewBag.ShroffId = shroff_book.shroff_id;//委托书id
                ViewBag.TableNumber = shroff_book.shroff_number;//委托书编号
                ViewBag.Client = shroff_book.client;//委托人
                ViewBag.be_appraiser = be_appraiser;//被鉴定人
                ViewBag.be_appraiser_nationality = nation.nationalityName;//族别
                ViewBag.Foren = foren;//法医临床学检查笔录信息
                //计算被鉴定人年龄
                string idcard = be_appraiser.ID_card.Substring(6, 8);
                string dt = DateTime.Now.ToString("yyyyMMdd");
                ViewBag.be_appraiser_age = ((Convert.ToInt32(dt) - Convert.ToInt32(idcard)).ToString()).Substring(0, 2);
                //法医临床学检查笔录信息 使用仪器
                List<string> useTheInstrument = new List<string>();
                if (foren.UseTheInstrument != null)
                {
                    string[] str = foren.UseTheInstrument.Split('*');
                    foreach (var item in str)
                    {
                        if(item != "")
                            useTheInstrument.Add(item);
                    }
                }
                ViewBag.UseTheInstrument = useTheInstrument;
            }
            return View();
        }

        //送达回证
        public ActionResult ReceiptServiceEvaluations(int? shroffId)
        {
            if (shroffId != null)
            {
                Shroff_book shroff_book = dal.Shroff_book.ToList().Find(l => l.shroff_id == shroffId);//查委托书
                ViewBag.ShroffId = shroff_book.shroff_id;
                ViewBag.TableNumber = shroff_book.shroff_number; ReceiptServiceEvaluations rece = dal.ReceiptServiceEvaluations.ToList().Find(l => l.ShroffId == shroffId);//通过委托书查回证
                ViewBag.Rece = rece;
                List<string> pingjia = new List<string>();
                if (rece.ShroffCircs != null || rece.ShroffPeople != null || rece.ShroffFinish != null || rece.ShroffContentOrRequire != null)
                {
                    pingjia.Add(rece.ShroffCircs);
                    pingjia.Add(rece.ShroffPeople);
                    pingjia.Add(rece.ShroffFinish);
                    pingjia.Add(rece.ShroffContentOrRequire);
                }
                pingjia = pingjia.Distinct().ToList();
                ViewBag.pingjia = pingjia;
            }
            return View();
        }

        //提交送达回证
        public ActionResult SubmitReceiptServiceEvaluations(ReceiptServiceEvaluations rece)
        {
            ReceiptServiceEvaluations _rece = dal.ReceiptServiceEvaluations.ToList().Find(l => l.ShroffId == rece.ShroffId);

            _rece.AmanuensisInstitution = rece.AmanuensisInstitution;
            _rece.ManuensisPeople = rece.ManuensisPeople;
            _rece.OtherData = rece.OtherData;
            _rece.InstitutionNumber = rece.InstitutionNumber;
            _rece.ReceiptMode = rece.ReceiptMode;
            _rece.TakePeople = rece.TakePeople;
            _rece.TabkeTime = rece.TabkeTime;
            _rece.RelationBoth = rece.RelationBoth;
            _rece.RelationPhone = rece.RelationPhone;
            _rece.AmanuensisPeople = rece.AmanuensisPeople;
            _rece.ToRejectReason = rece.ToRejectReason;
            _rece.AddresseeSign = rece.AddresseeSign;
            _rece.AddressDateTime = rece.AddressDateTime;
            _rece.BackTime = rece.BackTime; 
            _rece.Operator = rece.Operator;
            _rece.MailAddress = rece.MailAddress;
            _rece.PostCode = rece.PostCode;
            _rece.Contact = rece.Contact;
            _rece.Phone = rece.Phone;
            _rece.QQ = rece.QQ;
            _rece.Addressee = rece.Addressee;
            _rece.AmanuensisExplanation = rece.AmanuensisExplanation;
            _rece.ShroffCircs = rece.ShroffCircs;
            _rece.ShroffPeople = rece.ShroffPeople;
            _rece.ShroffFinish = rece.ShroffFinish;
            _rece.ShroffContentOrRequire = rece.ShroffContentOrRequire;
            _rece.Idea = rece.Idea;
            _rece.SurveillancePhone = rece.SurveillancePhone;
            dal.SaveChanges();
            
            Response.Redirect("/ShroffBook/shroffbookinfos?id=" + rece.ShroffId);
            return View();
        }

        //以数据展示送达回证
        public ActionResult ReceiptServiceEvaluationsDataView(int? id)
        {
            if (id != null)
            {
                Shroff_book shroff_book = dal.Shroff_book.ToList().Find(l => l.shroff_id == id);//查委托书
                ViewBag.ShroffId = shroff_book.shroff_id;
                ViewBag.TableNumber = shroff_book.shroff_number;
                ReceiptServiceEvaluations rece = dal.ReceiptServiceEvaluations.ToList().Find(l => l.ShroffId == id);//通过委托书查回证
                ViewBag.Rece = rece;
                string[] pingjia = new string[] { rece.ShroffCircs, rece.ShroffPeople, rece.ShroffFinish, rece.ShroffContentOrRequire };//评价
                ViewBag.pingjia = pingjia;
            }
            return View();
        }

       
    }
}